package com.monster.service;

import com.monster.dao.*;
import com.monster.pojo.Album;
import com.monster.pojo.Playlist;
import com.monster.pojo.Singer;
import com.monster.pojo.Song;
import com.monster.pojo.User;

import java.util.ArrayList;

public class UserServiceImpl implements UserService {
    private UserDao ud = new UserDaoImpl();
    private PlaylistDao pd = new PlaylistDaoImpl();

    @Override
    public User getUserById(Long userId) {
        return ud.getUserById(userId);
    }

    /**
     * 判断手机号是否已被注册，已注册返回用户id，未注册返回null
     * @param phoneNumber 手机号
     * @return 用户id或null
     */
    @Override
    public Long isRegistered(String phoneNumber) {
        return ud.getUserIdByPhone(phoneNumber);
    }

    /**
     * 判断手机号和密码是否正确，正确返回true，错误或不存在返回false
     * @param phoneNumber 手机号
     * @param password 密码
     * @return
     */
    @Override
    public Long toLogin(String phoneNumber, String password) {
        return ud.getUserIdByPhoneAndPassword(phoneNumber, password);
    }

    /**
     * 注册用户，成功返回true，失败返回false
     * @param user 用户信息
     * @return 成功-true，失败-false
     */
    @Override
    public boolean register(User user) {
        if (ud.addUser(user) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 更新用户信息，成功返回true，失败返回false
     * @param user 用户信息
     * @return 成功-true，失败-false
     */
    @Override
    public boolean updateUser(User user) {
        if (ud.updateUser(user) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 根据用户id和关键字获得用户的收藏（我喜欢）的歌曲、歌单或专辑的列表分页
     * @param userId 用户id
     * @param key 关键字，其中“song”为歌曲，“playlist”为歌单，“album”为专辑
     * @return 用户的收藏（我喜欢）的歌曲、歌单或专辑的列表
     */
    @Override
    public ArrayList<Object> listMyLike(Long userId, String key, int page) {
        ArrayList<Object> objects = new ArrayList<>();
        switch (key) {
            case "song":
                SongDao sd = new SongDaoImpl();
                ArrayList<Song> songs = ud.listLikeSongsByUserId(userId, page);
                for (Song song : songs) {
                    song.setSingers(sd.listSingersBySongId(song.getSongId()));
                    song.setAlbum(sd.getAlbumBySongId(song.getSongId()));
                }
                objects.addAll(songs);
                break;
            case "playlist":
                ArrayList<Playlist> playlists = ud.listLikePlaylistsByUserId(userId, page);
                for (Playlist p : playlists) {
                    p.setUser(pd.getFounderByPlaylistId(p.getPlaylistId()));
                    p.setSongCount(pd.getSongCount(p.getPlaylistId()));
                }
                objects.addAll(playlists);
                break;
            case "album":
                AlbumDao ad = new AlbumDaoImpl();
                ArrayList<Album> albums = ud.listLikeAlbumsByUserId(userId, page);
                for (Album album : albums) {
                    album.setSingers(ad.findSingersByAlbumId(album.getAlbumId()));
                    album.setSongCount(ad.findSongCountByAlbumId(album.getAlbumId()));
                }
                objects.addAll(albums);
                break;
            default:
        }
        return objects;
    }

    /**
     * 根据用户id和关键字获得用户关注的歌手或用户列表分页
     * @param userId 用户id
     * @param key 关键字，其中“singer”为歌手，“user”为用户
     * @param page 页码
     * @return 用户关注的歌手或用户列表
     */
    @Override
    public ArrayList<Object> listFocus(Long userId, String key, int page) {
        ArrayList<Object> objects = new ArrayList<>();
        switch (key) {
            case "singer":
                SingerDao sd = new SingerDaoImpl();
                ArrayList<Singer> singers = ud.listFocusSingesByUserId(userId, page);
                for (Singer s : singers) {
                    s.setFocusCount(sd.selectFocusBySingerId(s.getSingerId()));
                }
                objects.addAll(singers);
                break;
            case "user":
                ArrayList<User> users = ud.listFocusUsersByUserId(userId, page);
                for (User user : users) {
                    user.setFollow(ud.getFollowCountByUserId(userId));
                }
                objects.addAll(users);
                break;
            default:
        }
        return objects;
    }

    /**
     * 根据用户id分页查询获得粉丝用户的id、name、Head和关注数
     * @param userId 用户ID
     * @param page 页码
     * @return 粉丝用户的id、name和Head
     */
    @Override
    public ArrayList<User> listFans(Long userId, int page) {
        ArrayList<User> fans = ud.listFansByUserId(userId, page);
        for (User user : fans) {
            user.setFollow(ud.getFollowCountByUserId(user.getId()));
        }
        return fans;
    }

    /**
     * 新增歌单
     * @param playlist 歌单信息
     * @return 创建成功-true，创建失败-false
     */
    @Override
    public boolean addPlaylist(Playlist playlist) {
        if (pd.addPlaylist(playlist) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 更新歌单
     * @param playlist 更新后的歌单信息
     * @return 更新成功-true，更新失败-false
     */
    @Override
    public boolean updatePlaylist(Playlist playlist) {
        if (pd.updatePlaylist(playlist) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 删除歌单
     * @param playlistId 要删除的歌单id
     * @return 删除成功-true，删除失败-false
     */
    @Override
    public boolean deletePlaylist(Long playlistId) {
        if (pd.deletePlaylist(playlistId) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 将歌曲添加到歌单中
     * @param playlistId 歌单Id
     * @param songId 要添加的歌曲Id
     * @return 添加成功-true，失败-false
     */
    @Override
    public boolean addSongToPlaylist(Long playlistId, Long songId) {
        Playlist p = new Playlist();
        p.setPlaylistId(playlistId);
        if (pd.addPlaylistSong(p, songId) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 从歌单中删除歌曲
     * @param playlistId 歌单Id
     * @param songId 要删除的歌曲Id
     * @return 删除成功-true， 删除失败-false
     */
    @Override
    public boolean deleteSongFromPlaylist(Long playlistId, Long songId) {
        Playlist p = new Playlist();
        p.setPlaylistId(playlistId);
        if (pd.deletePlayListSongBySongId(p, songId) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 根据用户Id分页查询创建的歌单列表
     * @param userId 用户Id
     * @param page 页码
     * @return 创建的歌单集合
     */
    @Override
    public ArrayList<Playlist> listCreatedPlaylist(Long userId, int page) {
        return pd.listCreatedPlaylistByUserId(userId, page);
    }

    /**
     * 根据用户Id分页查询创建的歌单列表
     * @param userId 用户Id
     * @param page 页码
     * @return 创建的歌单集合
     * 包括空歌单
     */
    @Override
    public ArrayList<Playlist> listSetPlaylist(Long userId, int page) {
        return pd.listSetPlaylistByUserId(userId, page);
    }

}
